package demo.bit.list;

import java.util.Arrays;

public class MyArrayList {
    private int[] nums;
    private int usedSize;


    //初始化链表
    public MyArrayList() {
        this.nums = new int[3];
    }


    //打印链表
    public void printList() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(nums[i] + " ");
        }
        System.out.println();
    }

    //有效元素个数
    public int getUsedSize() {
        return this.usedSize;
    }

    //扩充链表
    public void expandList() {
        this.nums = Arrays.copyOf(this.nums, nums.length * 2);
    }

    //插入
    public void insert(int pos, int num) {
        int i = 0;
        if (pos <= 0 || pos > this.usedSize + 1) {
            System.out.println("插入失败 插入位置异常");
            return;
        }
        if (this.usedSize == this.nums.length) { //扩充链表
            expandList();
        }
        for (i = this.usedSize - 1; i >= pos - 1; i--) {
            this.nums[i + 1] = this.nums[i];
        }
        this.nums[i + 1] = num;
        this.usedSize++;
    }

    //判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.nums[i]) {
                return true;
            }
        }
        return false;
    }

    //查找某个元素对应的位置
    // -1表示没有该值
    public int search(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.nums[i]) {
                return i;
            }
        }
        System.out.println("查找失败");
        return -1;
    }

    //获取pos位置的元素
    public int getPos(int pos) {
        if (isEmpty()) {
            System.out.println("顺序表为空");
            return -1;
        }
        if (pos <= 0 || pos >= this.usedSize) {
            System.out.println("pos 位置不合法");
            return -1;  //不考虑数组中是-1元素的可能性
        }
        return this.nums[pos - 1];
    }

    public boolean isEmpty() {
        return this.usedSize == 0;
    }
    //给pos位置的元素设为value
    public void setPos(int pos, int value) {
        if (pos <= 0 || pos >= this.usedSize) {
            System.out.println("pos 位置不合法");
            return;
        }

        this.nums[pos - 1] = value;
    }

    //删除第一次出现的关键词key
    public void remove(int toRemove) {
        if (isEmpty()) {
            System.out.println("顺序表为空");
            return;
        }
        int index = 0;
        for (int i = 0; i < this.usedSize; i++) {
            if (this.nums[i] == toRemove) {
                index = i;
            }
        }
        for (int i = index; i < this.usedSize - 1; i++) {
                this.nums[i] =  this.nums[i + 1];
        }
        this.usedSize--;
        //this.nums[usedSize] = null;  如果数组中是引用数据类型   记得删完之后给最后一个元素置为空
        }

    //清空顺序表
    public void clear() {
        for (int i = 0; i < this.usedSize; i++) {
            this.nums[i] = 0;
        }
        //如果数组中是引用数据类型   这样写才能做到真正的清空顺序表
        /*for (int i = 0; i < usedSize; i++) {
            this.nums[i] = null;
        }*/

        this.usedSize = 0;
    }
}
